<?php
namespace app\model;
use app\model\UserOrder as ModelUserOrder;
use app\model\UserInfo as ModelUserInfo;
use app\model\CourierInfo as ModelCourierInfo;
use app\model\StationInfo as ModelStationInfo;
use app\model\DriverInfo as ModelDriverInfo;
use app\model\StationMemberInfo as ModelStationMemberInfo;



use function PHPSTORM_META\map;
use think\Db;
use think\Request;

class UserOrderLog extends Common
{
    protected static $obj;

    public function _initialize()
    {
        parent::_initialize();
        $this->db_name = 'user_order_log';
    }

    /**
     * 类入口
     * @return UserOrderLog
     */
    public static function getInstance()
    {
        if (is_null(self::$obj))
            self::$obj = new self();
        return self::$obj;
    }

    /*
     * 写入订单日志
     * operator: 1:快递员；2：司机；3：站点；4：用户;5：后台管理员；0：其他
     * mapId : 对应操作员id  如果是站点，mapId为站点员工id；对应station_member_info的id
     */
    public function addOrderLog($orderId,$operator,$mapId,$remark = '')
    {
        $data['order_id'] = $orderId;
        $data['operator'] = $operator;
        $data['map_id'] = $mapId;
        $data['order_state'] = $this->getOrderState($orderId);
        $data['order_state_display'] = $this->getOrderStateDisplay($orderId);
        $data['pay_state'] = $this->payStateDisplay($orderId);
        $data['mobile'] = $this->getMobile($operator,$mapId);
        $data['remark'] = $remark;
        $data['describe'] = $this->getDescribe($orderId);
        $data['order_number'] = $this->getOrderSn($orderId);

        return $this->toAdd($data);
    }
    /*
     * 获取describe
     */
    public function getDescribe($orderId){
        //-1：系统取消订单；0：客户已取消，5：订单异常；10，待支付；20：未支付，已接单；25：未支付，已取件；30：已支付，已接单，已取件；40：已入库；50:封箱；60：出库；65：中转；70：配送员接单；75：配送员已取件；80：配送完成；90：配送员取消;95:已删除'
        $display = $this->getOrderState($orderId);
        switch ($display){
            case -1 :
                $describe = '系统取消订单';
                break;
            case 0 :
                $describe = '订单已取消，期待再次为您服务';
                break;
            case 5 :
                $describe = '订单异常';
                break;
            case 10 :
                $describe = '订单已提交，等待取件';
                break;
            case 20 :
                $describe = '快递员已接单，请耐心等候';//
                break;
            case 25 :
                $describe = '核验快递信息，确认取件中';//快递员已接单，请耐心等候
                break;
            case 30 :
                $describe = '快递员已取件';
                break;
            case 40 :
                $describe = '物品已进入站点仓库分拣';
                break;
            case 50 :
                $describe = '已装箱';
                break;
            case 60 :
                $describe = '等待快递接单';//出库
                break;
            case 65 :
                $describe = '快递物品已经装箱，准备发往XXX站点';
                break;
            case 70 :
                $describe = '等待快递接单';//配送员接单
                break;
            case 75 :
                $describe = '快递员已接单派送，请耐心等候';//配送员已取件
                break;
            case 80 :
                $describe = '客户签名';//已签收
                break;
            case 90 :
                $describe = '配送员取消';
                break;
            case 95 :
                $describe = '已删除';
                break;
            default :
                break;
        }
        return $describe;

    }
    /*
     * 获取手机号
     */
    public function getMobile($operator,$mapId){

        switch ($operator){
            case 1 :
                $mobile = ModelCourierInfo::getInstance()->toValue(['id'=>$mapId],'mobile');
                break;
            case 2 :
                $mobile = ModelDriverInfo::getInstance()->toValue(['id'=>$mapId],'mobile');
                break;
            case 3 :
                $mobile = ModelStationMemberInfo::getInstance()->toValue(['id'=>$mapId],'mobile');
                break;
            case 4 :
                $mobile = ModelUserInfo::getInstance()->toValue(['user_id'=>$mapId],'mobile');
                break;
            default :
                $mobile = '';
                break;
        }
        return $mobile;
    }

    /*
     * 获取订单状态
     */
    public function getOrderState($orderId){

       $orderState = ModelUserOrder::getInstance()->toValue(['order_id'=>$orderId],'order_state');
       return $orderState;

    }
    /*
     * 获取订单号
     */
    public function getOrderSn($orderId)
    {
        return ModelUserOrder::getInstance()->toValue(['order_id'=>$orderId],'order_sn as orderSn');
    }
    /*
     * 获取订单状态描述
     */
    public function getOrderStateDisplay($orderId)
    {
        //-1：系统取消订单；0：客户已取消，5：订单异常；10，待支付；20：未支付，已接单；25：未支付，已取件；30：已支付，已接单，已取件；40：已入库；50:封箱；60：出库；65：中转；70：配送员接单；75：配送员已取件；80：配送完成；90：配送员取消;95:已删除'
        $display = $this->getOrderState($orderId);
        switch ($display){
            case -1 :
                $orderStateDisplay = '系统取消订单';
                break;
            case 0 :
                $orderStateDisplay = '客户已取消';
                break;
            case 5 :
                $orderStateDisplay = '订单异常';
                break;
            case 10 :
                $orderStateDisplay = '已提交';
                break;
            case 20 :
                $orderStateDisplay = '已接单';
                break;
            case 25 :
                $orderStateDisplay = '未支付，已取件';
                break;
            case 30 :
                $orderStateDisplay = '已取件';
                break;
            case 40 :
                $orderStateDisplay = '已入库';
                break;
            case 50 :
                $orderStateDisplay = '已装箱';
                break;
            case 60 :
                $orderStateDisplay = '待接单';//出库
                break;
            case 65 :
                $orderStateDisplay = '中转';
                break;
            case 70 :
                $orderStateDisplay = '已接单';//配送员接单
                break;
            case 75 :
                $orderStateDisplay = '待派件';//配送员已取件
                break;
            case 80 :
                $orderStateDisplay = '已签收';//已签收
                break;
            case 90 :
                $orderStateDisplay = '配送员取消';
                break;
            case 95 :
                $orderStateDisplay = '已删除';
                break;
            default :
                break;
        }
        return $orderStateDisplay;
    }
    /*
    * 获取订单状态描述
    */
    public function payStateDisplay($orderId)
    {
        //-1：系统取消订单；0：客户已取消，5：订单异常；10，待支付；20：未支付，已接单；25：未支付，已取件；30：已支付，已接单，已取件；40：已入库；50:封箱；60：出库；65：中转；70：配送员接单；75：配送员已取件；80：配送完成；90：配送员取消;95:已删除'
        $display = $this->getOrderState($orderId);
        switch ($display){
            case -1 :
                $orderPayDisplay = '系统取消订单';
                break;
            case 0 :
                $orderPayDisplay = '客户已取消';
                break;
            case 5 :
                $orderPayDisplay = '订单异常';
                break;
            case 10 :
                $orderPayDisplay = '待付款';
                break;
            case 20 :
                $orderPayDisplay = '待付款';
                break;
            case 25 :
                $orderPayDisplay = '待付款';
                break;
            case 30 :
                $orderPayDisplay = '已支付';
                break;
            case 40 :
                $orderPayDisplay = '已支付';
                break;
            case 50 :
                $orderPayDisplay = '已支付';
                break;
            case 60 :
                $orderPayDisplay = '已支付';
                break;
            case 65 :
                $orderPayDisplay = '已支付';
                break;
            case 70 :
                $orderPayDisplay = '已支付';
                break;
            case 75 :
                $orderPayDisplay = '已支付';
                break;
            case 80 :
                $orderPayDisplay = '已支付';
                break;
            case 90 :
                $orderPayDisplay = '已支付';
                break;
            case 95 :
                $orderPayDisplay = '已支付';
                break;
            default :
                break;
        }
        return $orderPayDisplay;
    }



}
